package sqlib;
//import Employee;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import sqlib.annotations.DbName;


public  class SQLGenerator {

	private DataMapper mapper;
	private PrimaryKeyProvider primaryKeyProvider;
	
	public SQLGenerator(DataMapper mapper) {
		this.mapper = mapper;
		primaryKeyProvider = new IDPrefixPrimaryKeyProvider();
	}
	
	public void setMapper(DataMapper mapper) {
		this.mapper = mapper;
	}
	
	public void setPrimaryKeyProvider(PrimaryKeyProvider primaryKeyFinder) {
		this.primaryKeyProvider = primaryKeyFinder;
	}
	
	public String createTableInstruction(Class<?> c) {
		
		//Ponto 2
		SQLCreateTable sql = c.isAnnotationPresent(DbName.class) ? 
				new SQLCreateTable(c.getAnnotation(DbName.class).value()) : 
					new SQLCreateTable(c.getSimpleName()); 
		
		
		
		for(Field f : c.getDeclaredFields()) {
			String type = mapper.convertType(f.getType());
			
			//Ponto 2
			String columnName;
			if(f.isAnnotationPresent(DbName.class)) {
				columnName = f.getAnnotation(DbName.class).value();
			}
			else {
				columnName = f.getName();
			}
			
			if(primaryKeyProvider.isPrimaryKey(f)) {
				String fieldName = primaryKeyProvider.getPrimaryKeyID(f);
				sql.column(fieldName, type).asPrimaryKey();
			}
			else
				sql.column(columnName, type);
		}
		
		return sql.getSQL();
	}

	public String createInsertStatement(Object object) {
		Class<?> c = object.getClass();
		String sql = "INSERT INTO " + c.getSimpleName() + " VALUES (";
		Field[] fields = c.getDeclaredFields();
		
		for(int i = 0; i < fields.length; i++) {
			if(i != 0)
				sql += ", ";
			try {
				fields[i].setAccessible(true);
				Object obj = fields[i].get(object);	
				sql += convertToSQL(obj);
				
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		sql += ");";
		return sql;
	}

	public List<Object> select(Class<?> c) {
		
//		String sql = "SELECT ";
//		
//		for (Field f : c.getDeclaredFields()) {
//			sql += f.getName() + ", ";
//		}
//		
//		sql += "FROM " + c.getSimpleName();
//		
//		return sql;
				
		List<String[]> list = DBSimulator.getAllRecords(c.getSimpleName());
		
		try {
						
			Object obj = null;		
			Field field = null;
			
			List<Object> employees = new ArrayList<Object>();
			
			for (String[] strings : list) {
				obj = c.newInstance();
				
				field = c.getDeclaredField("id_number");
				field.setInt(c, Integer.parseInt(strings[0]));
				
				field = c.getDeclaredField("name");
				field.set(c, strings[1]);
				
				employees.add(obj);
			}
			
			
			
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		for(String[] record : list)
			System.out.println(Arrays.toString(record));
		
		return null;
	}
	
	private String convertToSQL(Object obj) {
		if(obj.getClass().equals(String.class))
			return "'" + obj.toString() + "'";
		
		return obj.toString();
	}
	
	
	
	
	
}
